package com.appspot.didit.client.user;

import com.appspot.didit.client.BaseView;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.inject.Inject;

public class AuthenticationPresenterImpl implements AuthenticationPresenter {
	
    private final View view;
    
    private final LoginPresenter loginPresenter;
    
    private final RegisterPresenter registerPresenter;
   
    @Inject
    public AuthenticationPresenterImpl(final HandlerManager eventBus, LoginPresenter loginPresenter, RegisterPresenter registerPresenter, View view) {
    	this.view = view;
    	
    	this.loginPresenter = loginPresenter;

    	this.registerPresenter = registerPresenter;
    	
        bindEvents(eventBus);
    }
    
    private void bindEvents(HandlerManager eventBus) {
        eventBus.addHandler(LogoutEvent.getType(),
                new LogoutHandler() {
                        public void onLogout(LogoutEvent event) {
                                doLogout();
                        }
                });

	}
	
	protected void doLogout() {
		view.showAuthentication();
	}
	
    public void go(RootPanel rootPanel) {
    	view.addLogin(loginPresenter.getView());
    	view.addRegister(registerPresenter.getView());
        rootPanel.add(view.getWidget());
    }

    public BaseView getView() {
            return view;
    }

}